/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Biblioteca;

import BancoDeDados.ConexaoMySQL;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JOptionPane;

/**
 * Library
 *
 * Versao 1.0
 *
 * Data 24/01/2013
 *
 * @author Severo
 */
public class Library {

    //Construtores
    private Library() {
    }

    //Outros Metodos  
    /**
     * * Centraliza o Frame ou internalFrame passado como parametro
     *
     * @param frame se for passado, ira centralizar o Frame de acordo com o
     * Screen
     * @param internalFrame se for passado, ira centralizar o internalFrame de
     * acordo com o JDesktopPane
     * @param desktopPane serve de parametro para centralizar o internalFrame
     */
    public static void centralizarFrameOrInternalFrame(JFrame frame, javax.swing.JDesktopPane desktopPane, JInternalFrame internalFrame) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();

        if (internalFrame == null) {
            int screenWidth = screenSize.width - frame.getWidth();
            int screenHeigth = screenSize.height - frame.getHeight();
            frame.setLocation(screenWidth / 2, screenHeigth / 2);
        } else {
            int screenWidth = desktopPane.getWidth() - internalFrame.getWidth();
            int screenHeigth = desktopPane.getHeight() - internalFrame.getHeight();
            internalFrame.setLocation(screenWidth / 2, screenHeigth / 2);
        }
    }

    /**
     * Pega a versao do software no arquivo de configuracao
     * @return 
     */
    public static String getVersaoTREF() {
        Properties config = new Properties();
        String arquivoProperties = "./configTREF.properties";
        try {
            config.load(new FileInputStream(arquivoProperties));
        } catch (IOException ex) {
            Logger.getLogger(ConexaoMySQL.class.getName()).log(Level.SEVERE, null, ex);
        }

        return config.getProperty("TREF_versao");
    }

    /**
     * Faz com que o botao, passado como parametro, reaja ao clique de ENTER com
     * um clique normal
     *
     * @param btn
     */
    public static void reagirEnter(JButton btn) {
        btn.registerKeyboardAction(
                btn.getActionForKeyStroke(
                        KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED
        );

        btn.registerKeyboardAction(
                btn.getActionForKeyStroke(
                        KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED
        );
    }

    /**
     * Limpa todos JTextField que sao componentes do JPanel
     * @param panel 
     */
    public static void limpaTextos(JPanel panel) {
        //Todos os componentes do Painel do Frame  
        Component components[] = panel.getComponents();
        for (Component component : components) {
            //É campo de texto?
            if (component instanceof JTextField) {
                //Está preenchido?
                ((JTextField) component).setText("");
            }
        }
    }

    /**
     * Cria o JInternalFrame dentro de um JDialog modal, simulando um JIternalFrame modal
     * @param dlg
     * @param internalFrame 
     */
    public static void criaInternalFrameDentroDialog(JDialog dlg, JInternalFrame internalFrame){
        dlg.setTitle(internalFrame.getTitle());
        //Faz o JDialog receber os mesmo dados do JIternalFrame
        dlg.setContentPane(internalFrame.getContentPane());
        //Redimensiona
        dlg.setSize(internalFrame.getWidth(), internalFrame.getHeight()+5); //+5, pois estava cortando uma parte da tela
        //Impede que o usuario redimensione a tela
        dlg.setResizable(false);
        //Centraliza
        dlg.setLocationRelativeTo(null);
        dlg.setModal(true);
        dlg.setVisible(true);
    }

    /**
     * Impede a digitacao de caracteres especiais, permite a digitacao
     * apenas dos caracters especificado no metodo
     * permite o @ e o _
     * @param textField 
     */
    public static void semCaracterEspecial(JTextField textField){
        textField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent arg0) {
                char c = arg0.getKeyChar();
                if (     !((c >= 48) && (c <= 57) //de 0 ate 9
                        || (c == 64) //@
                        || (c == 46) //.
                        || (c >= 65) && (c <= 90) //de a ate z
                        || (c == 95) //_
                        || (c >= 97) && (c <= 122) // de A ate Z
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)
                        || (c == KeyEvent.VK_ENTER)
                        || (c == KeyEvent.VK_SPACE))){
                    arg0.consume();
                    //JOptionPane.showMessageDialog(null, (int) c, "Atenção!", JOptionPane.INFORMATION_MESSAGE);
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }

    /**
     * Impede a digitacao da BarraDeEspaco
     * @param textField 
     */
    public static void semBarraDeEspaco(JTextField textField){
        textField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent arg0) {
                char c = arg0.getKeyChar();
                if ((c == KeyEvent.VK_SPACE)){
                    arg0.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }
}
